
import SQLite

class DbMember : DbBase {
    var member: Table = Table("member")
    let _id = Expression<Int64>("id")
    let _groupId = Expression<String>("groupId")
    let _memberId = Expression<String>("memberId")
    let _alias = Expression<String>("alias")
    let _fixAlias = Expression<String>("fixAlias")
    let _extra = Expression<String>("extra")
    let _type = Expression<Int>("type")
    let _mute = Expression<Int>("mute")
    let _timestamp = Expression<Int64>("timestamp")
    let _groupId_memberId = Expression<String>("_groupId_memberId")
    
    func open(){
        do{
            try getDb()!.run(member.create(ifNotExists: true) { t in
                t.column(_id, primaryKey: .autoincrement)
                t.column(_groupId)
                t.column(_memberId)
                t.column(_alias)
                t.column(_fixAlias)
                t.column(_extra)
                t.column(_type)
                t.column(_mute)
                t.column(_timestamp)
                t.column(_groupId_memberId, unique: true)
            })
            try getDb()!.run(member.createIndex(_groupId, _memberId, unique: true, ifNotExists: true))

        }catch{
            print("\(error)")
        }
    }
    func getMemberInfo(_ groupId:String, _ memberId:String) -> DMCCMember? {
        do{
            for members in try getDb()!.prepare(member.filter(groupId == _groupId && memberId == _memberId)) {
                let u = DMCCMember()
                u.id = members[_id]
                u.isNull = false
                u.groupId = members[_groupId]
                u.memberId = members[_memberId]
                u.alias = members[_alias]
                u.fixAlias = members[_fixAlias]
                u.extra = members[_extra]
                u.type = OsnMemberType(rawValue: members[_type])
                u.mute = members[_mute]
                return u
            }
        }catch{
            print("\(error)")
        }
        return nil
    }
    func addMemberInfo(_ info:DMCCMember) -> Bool {
        do{
            return try getDb()!.run(member.upsert(
                _groupId <- info.groupId,
                _memberId <- info.memberId,
                _alias <- info.alias,
                _fixAlias <- info.fixAlias,
                _extra <- info.extra,
                _type <- info.type.rawValue,
                _mute <- info.mute,
                _timestamp <- info.timestamp,
                _groupId_memberId <- info.groupId+info.memberId,
                onConflictOf: _groupId_memberId)) >= 0
        }catch{
            print("\(error)")
        }
        return false
    }
    func clearMemberInfo(_ groupId:String) -> Bool {
        do{
            return try getDb()!.run(member.filter(_groupId == groupId).delete()) > 0
        }catch{
            print("\(error)")
        }
        return false
    }
    func delMemberInfo(_ groupId:String, _ memberId:String) -> Bool {
        do{
            return try getDb()!.run(member.filter(_groupId == groupId && _memberId == memberId).delete()) > 0
        }catch{
            print("\(error)")
        }
        return false
    }
    func listMemberWithIndex(_ osnId:String, _ begin:Int, _ count:Int) ->[DMCCMember] {
        var ms:[DMCCMember] = []
        do{
            for members in try getDb()!.prepare(member.filter(_groupId == osnId && _id > Int64(begin)).limit(count)) {
                let u = DMCCMember()
                u.id = members[_id]
                u.isNull = false
                u.groupId = members[_groupId]
                u.memberId = members[_memberId]
                u.alias = members[_alias]
                u.fixAlias = members[_fixAlias]
                u.extra = members[_extra]
                u.type = OsnMemberType(rawValue: members[_type])
                u.mute = members[_mute]
                ms.append(u)
            }
        }catch{
            print("\(error)")
        }
        return ms
    }
}
let dbMember = DbMember()
